package net.valerijtimakov.homefinances.utils;

import java.util.Collection;

public class Utils {

	public static String implode(String glue, Object ... elements) {
	    if (elements == null || elements.length == 0) {	
	    	return "";
	    };
	    StringBuilder sb = new StringBuilder();
	    for (Object element : elements) {
	    	if (element != null) {
		    	String elStr = element.toString();
		    	if (!elStr.isEmpty()) {
		    		if (sb.length() > 0) {
		    			sb.append(glue);
		    		}
		    		sb.append(elStr);
		    	}
	    	}
	    }
	    return sb.toString();
	}

	public static void implode(String glue, StringBuilder res, Object ... elements) {
	    if (elements == null || elements.length == 0) {	
	    	return;
	    };
	    boolean addedYet = false;
	    for (Object element : elements) {
	    	if (element != null) {
		    	String elStr = element.toString();
		    	if (!elStr.isEmpty()) {
		    		if (addedYet) {
		    			res.append(glue);
		    		}
		    		res.append(elStr);
		    		addedYet = true;
		    	}
	    	}
	    }
	}
	
	public static <T> String implode(String glue, Collection<T> elements) {
	    if (elements == null || elements.size() == 0) {	
	    	return "";
	    };
	    StringBuilder sb = new StringBuilder();
	    for (Object element : elements) {
	    	if (element != null) {
		    	String elStr = element.toString();
		    	if (!elStr.isEmpty()) {
		    		if (sb.length() > 0) {
		    			sb.append(glue);
		    		}
		    		sb.append(elStr);
		    	}
	    	}
	    }
	    return sb.toString();
	}
	
	public static <T> void implode(String glue, StringBuilder res, Collection<T> elements) {
	    if (elements == null || elements.size() == 0) {	
	    	return;
	    };
	    boolean addedYet = false;
	    for (Object element : elements) {
	    	if (element != null) {
		    	String elStr = element.toString();
		    	if (!elStr.isEmpty()) {
		    		if (addedYet) {
		    			res.append(glue);
		    		}
		    		res.append(elStr);
		    		addedYet = true;
		    	}
	    	}
	    }
	}
	
	public static long[] longCollectionToArray(Collection<Long> srcColl) {
		long[] res = new long[srcColl.size()];
		int i = 0;
		for (long val : srcColl) {
			res[i] = val;
			i++;
		}
		return res;
	}
	
	public static Long[] longArrToRef(long[] src) {
		Long[] res = new Long[src.length];
		for (int i = 0; i < src.length; i++) {
			res[i] = src[i];
		}
		return res;
	}
}












